angular.module('tab-home', [])

.controller 'TabHomeCtrl', (Toast,$location, $localStorage, $ionicLoading, $scope, $state,$http, $window, $ionicHistory,$stateParams, orderService, ListDataService, $ionicScrollDelegate,$filter) ->
  orderListSrv = ListDataService.get('orderList')
  orderListSrv.watch (orderDatas) ->
    $scope.orderList = orderDatas

  getUserInfo = ->
    $http.get('/crew/info')
    .success (resp)->
      if resp.code == 0
        $localStorage.crew = resp.message
      else
        Toast.showMsg("获取数据失败")
    .error (resp, status) ->
      console.log(resp)
      Toast.showMsg("加载数据失败")
  getUserInfo()

  _.extend $scope,
    toDetail :(orderId)->
      $state.go('tab.orderDetail', {id: orderId})

    canLoadMore: () ->
      console.log(orderListSrv.canLoadMore())
      orderListSrv.canLoadMore()

    doRefresh: ->
      console.log 'reload'
      orderListSrv.reload({when: 3}, true).finally ->
        console.log 'load end'
        $scope.$broadcast 'scroll.refreshComplete'

    loadMore: ->
      console.log 'loadMore'
      orderListSrv.loadMore({when: 3}, true).finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'


  $scope.doRefresh()